//
//  LoginViewController.m
//  Lovely Food
//
//  Created by 李经纬 on 15/11/30.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "LoginHelper.h"
@interface LoginViewController ()<LoginHelperDelegate,UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userName; // 账户
@property (weak, nonatomic) IBOutlet UITextField *passWord; // 密码
@property (weak, nonatomic) IBOutlet UILabel *noEmptyUserName;
@property (weak, nonatomic) IBOutlet UILabel *noEmptyPassWord;
@property (weak, nonatomic) IBOutlet UILabel *errorUserNameAndPassWord;
@property (weak, nonatomic) IBOutlet UIImageView *backImage;



//@property (weak, nonatomic) IBOutlet UIButton *loginButton; // 登陆按钮
//@property (weak, nonatomic) IBOutlet UIButton *registerUserButton; // 注册用户
//@property (weak, nonatomic) IBOutlet UIButton *forgotPasswordButton; // 找回密码
//@property (weak, nonatomic) IBOutlet UIButton *tencentButton; // QQ登陆
//@property (weak, nonatomic) IBOutlet UIButton *wechatButton; // 微信登陆
//@property (weak, nonatomic) IBOutlet UIButton *sinaButton; // 新浪登陆
//@property (weak, nonatomic) IBOutlet UIButton *browseButton; // 游客模式

@end

@implementation LoginViewController

- (void)dealloc
{
    self.passWord.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view sendSubviewToBack:self.backImage];
    
    UIImageView *leftImageUser = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yonghuming"]];
    leftImageUser.frame = CGRectMake(0, 0, 20, 20);
    self.userName.leftView = leftImageUser;
    self.userName.delegate = self;
    self.userName.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIImageView *leftImagePass = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mima"]];
    leftImagePass.frame = CGRectMake(0, 0, 20, 20);
    self.passWord.leftView = leftImagePass;
    self.passWord.delegate = self;

    self.passWord.leftViewMode = UITextFieldViewModeAlways;

//    NSLog(@"%@",self.view.subviews);
    
    // Do any additional setup after loading the view from its nib.
}


// 登陆
- (IBAction)loginButtonAction:(UIButton *)sender {
    
    self.noEmptyPassWord.hidden = YES;
    self.noEmptyUserName.hidden = YES;
    self.errorUserNameAndPassWord.hidden = YES;
    NSInteger number = 0;
    if (self.userName.text.length == 0) {
        self.noEmptyUserName.hidden = NO;
        number = 1;
    }
    
    if (self.passWord.text.length == 0) {
        self.noEmptyPassWord.hidden = NO;
        number = 1;
    }
    
    if (number == 0) {
        // 登陆
        LoginHelper *login  = [[LoginHelper alloc] init];
        login.delegate = self;
        [self.view endEditing:YES];
        [login loginWithUserName:self.userName.text passWord:self.passWord.text];
        
        
    }
    
    
}
// 完成loginManaged的Delegate
- (void)promptView
{
    self.errorUserNameAndPassWord.hidden = NO;
    
}

// 点击取消键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
// 注册新用户
- (IBAction)registerUserButtonAction:(UIButton *)sender {
    
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self showViewController:registerVC sender:nil];
}
// 找回密码
- (IBAction)forgotPasswordButtonAction:(UIButton *)sender {
    
    UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"找回密码" message:@"输入邮箱地址,注意查看邮箱邮件" preferredStyle:UIAlertControllerStyleAlert];
    [aler addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入邮箱";
    }];
    UIAlertAction *promptAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"%@",aler.textFields[0].text);
        
        
        
        
    }];
    
    
    
    [aler addAction:promptAction];
    [aler addAction:confirmAction];
    
    [self presentViewController:aler animated:YES completion:^{
        
    }];
    
    
    
}
// 第三方登陆按钮
- (IBAction)thirdButtonAction:(UIButton *)sender {
    
}
// 游客模式
- (IBAction)browseButttonAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

// UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == self.userName) {
        [self.passWord becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    
    return YES;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
